package com.haredot.config;

import com.haredot.aspectj.LogAspect;
import com.haredot.properties.LogProperties;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@ConditionalOnClass(Aspect.class)
@EnableConfigurationProperties(LogProperties.class)
public class LogAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(LogAspect.class)
    @ConditionalOnProperty(prefix = "sys.log.service-log", name = "enable", matchIfMissing = true)
    @DependsOn("rabbitTemplate")
    @ConditionalOnClass(RabbitTemplate.class)
    public LogAspect logAspect(LogProperties logProperties, RabbitTemplate rabbitTemplate) {
        return new LogAspect(logProperties, rabbitTemplate);
    }
}
